package org.cy.evolution.net.http.converter.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;

public class FastJsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {

    private Type type;
    private ParserConfig config;
    private int featureValues;
    private Feature[] features;

    public FastJsonResponseBodyConverter(Type type, ParserConfig config, int featureValues,
                                         Feature... features) {
        this.type = type;
        this.config = config;
        this.featureValues = featureValues;
        this.features = features;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        try {
            return JSON.parseObject(value.string(), type, config, featureValues, features);
        } finally {
            value.close();
        }
    }
}
